package com.ideal.manage.esms.service;

import org.apache.commons.lang3.math.NumberUtils;

import java.time.Instant;
import java.time.LocalDateTime;
import java.util.List;
import java.util.TimeZone;

//电费价格区间
public class TimeRange {
    private int start;
    private int end;
    private double value;

    public TimeRange(String time, double value) {
        String startAndEnd[] = time.split("-");
        this.start = NumberUtils.toInt(startAndEnd[0].split(":")[0]);
        this.end = NumberUtils.toInt(startAndEnd[1].split(":")[0]);
        this.value = value;
    }

    public int getStart() {
        return start;
    }

    public void setStart(int start) {
        this.start = start;
    }

    public int getEnd() {
        return end;
    }

    public void setEnd(int end) {
        this.end = end;
    }

    public double getValue() {
        return value;
    }

    public void setValue(double value) {
        this.value = value;
    }

    public static double getValue(List<TimeRange> ranges, long timestamp) {
        LocalDateTime dateTime = LocalDateTime.ofInstant(Instant.ofEpochMilli(timestamp), TimeZone
                .getDefault().toZoneId());
        for (TimeRange range : ranges) {
            if (range.isMatch(dateTime.getHour())) {
                return range.value;
            }
        }
        return 0;
    }

    private boolean isMatch(int hour) {
        if (hour >= start && hour <= end) {
            return true;
        }
        return false;
    }
}
